package com.zx.itrack;

import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.RxHttpUtils;
import com.allen.library.config.OkHttpConfig;
import com.allen.library.gson.GsonAdapter;
import com.allen.library.interceptor.Level;
import com.allen.library.interceptor.LoggingInterceptor;

import com.czy.basecommon.utils.global.ItrackApp;
import com.github.jokar.multilanguages.library.MultiLanguage;
import com.tencent.bugly.crashreport.CrashReport;
import com.xuexiang.xui.XUI;


import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 *desc:APPcation
 *author:mid
 *addtime:2020/6/18 17:40
 */
public class App extends ItrackApp {

    @Override
    public void onCreate() {
        super.onCreate();

        initRxHttpUtils();
        ARouter.init(this);
        ARouter.openDebug();
        XUI.init(this); //初始化UI框架
        XUI.debug(true);  //开启UI框架调试日志
        CrashReport.initCrashReport(getApplicationContext());

    }

    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }
    private void initRxHttpUtils() {

        RxHttpUtils
                .getInstance()
                .init(this)
                .config()
                //自定义factory的用法
                .setConverterFactory(ScalarsConverterFactory.create())
                .setConverterFactory(GsonConverterFactory.create(GsonAdapter.buildGson()))
                .setCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //

                //开启全局配置
                .setOkClient(createOkHttp());


    }

    private OkHttpClient createOkHttp() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();


        OkHttpClient okHttpClient = new OkHttpConfig
                .Builder(this)
                .setCache(false)
                .setHasNetCacheTime(10)//默认有网络时候缓存60秒
                .setReadTimeout(20)
                //全局超时配置
                .setWriteTimeout(20)

                .setAddInterceptor(new LoggingInterceptor
                        .Builder()//构建者模式
                        .loggable(BuildConfig.DEBUG) //是否开启日志打印
                        .setLevel(Level.BASIC) //打印的等级
                        .log(Platform.INFO) // 打印类型
                        .request("Request") // request的Tag
                        .response("Response")// Response的Tag
                        .addHeader("log-header", "I am the log request header.") // 添加打印头, 注意 key 和 value 都不能是中文
                        .build())
                //全局超时配置
                .setConnectTimeout(15)
                //全局是否打开请求log日志
                .setDebug(BuildConfig.DEBUG)
                .build();

        return okHttpClient;
    }
}

